// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;


@ccclass
export default class NewClass extends cc.Component {
    // 地图组件
    @property(cc.Node)
    mapNode: cc.Node = null;

    // enemy预制体
    @property(cc.Prefab)
    enemyPrefab: cc.Prefab = null;

    // enemy amount
    @property
    enemyAmount: number = 3;

    // 计数器
    @property
    tt: number = 0;



    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        cc.director.getPhysicsManager().enabled = true;
        // cc.director.getPhysicsManager().debugDrawFlags = 1;

        // 碰撞组件
        cc.director.getCollisionManager().enabled = true
        // cc.director.getCollisionManager().enabledDebugDraw = true

        
        this.initMapNode(this.mapNode)

        // 实际敌人少一个 因为有一个隐藏的没显示
        this.enemyAmount = 5;

        // this.createEnemy()
    }

    // start () {

    // }

    update (dt:number) {

        // 间隔检验当前敌人数量
        this.tt += dt
        if(this.tt > 1) {
            this.createEnemy()
            this.tt = 0
        }

    }


    createEnemy():void{
        // 获取当前敌人数量
        let noeds = this.node.children
        let enemyNodes = noeds.filter(e => e.name == 'enemy')

        if(enemyNodes.length < this.enemyAmount) {
            // 补齐敌人
            let amount = this.enemyAmount - enemyNodes.length
            for (let i = 0; i < amount; i++) {
                let enemy = cc.instantiate(this.enemyPrefab)
                // 随机坐标
                enemy.setPosition(cc.v2(this.randomRange(-150, 150), this.randomRange(-34, 170)))
                this.node.addChild(enemy)
            }
        }

    }


    /**
     * 初始化地图物理碰撞
     * @param mapNode 
     */
    initMapNode(mapNode: cc.Node):void {
        let tiledMap = mapNode.getComponent(cc.TiledMap)
        let tiledSize = tiledMap.getTileSize()
        let layer = tiledMap.getLayer('wall')
        let layerSize = layer.getLayerSize()

        for (let i = 0; i < layerSize.width; i++) {
            for (let j = 0; j < layerSize.height; j++) {
                let tiled = layer.getTiledTileAt(i, j, true)
                if(tiled.gid != 0) {
                    tiled.node.group = 'wall'

                    let body = tiled.node.addComponent(cc.RigidBody)
                    body.type = cc.RigidBodyType.Static

                    let collider = tiled.node.addComponent(cc.PhysicsBoxCollider)
                    collider.offset = cc.v2(tiledSize.width / 2, tiledSize.height / 2)
                    collider.size = tiledSize
                    collider.apply()
                }
                
            }
        }
    }


    randomRange(min:number, max:number):number {
        return Math.random() * (max - min) + min;
      }
}
